package com.lange.fileManagement.utils;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class TokenGenerator {
    // Define the characters that will be used in the token
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    // Define the length of the token
    private static final int TOKEN_LENGTH = 32;

    public static void main(String[] args) {
        try {
            // Generate a random token
            String token = generateToken(TOKEN_LENGTH);
            System.out.println("Generated Token: " + token);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static String generateToken(int length) throws NoSuchAlgorithmException {
        SecureRandom random = SecureRandom.getInstanceStrong();
        StringBuilder token = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHARACTERS.length());
            token.append(CHARACTERS.charAt(index));
        }

        return token.toString();
    }
}